package com.gw.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gw.mapper.PostMapper;
import com.gw.mapper.UserMapper;
import com.gw.pojo.dto.PostDTO;
import com.gw.pojo.dto.ReplyDTO;
import com.gw.pojo.entity.Post;
import com.gw.pojo.entity.Reply;
import com.gw.pojo.vo.PostDetailVO;
import com.gw.pojo.vo.PostVO;
import com.gw.result.PageResult;
import com.gw.service.PostService;
import com.gw.utils.MinioUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class PostServiceImpl  implements PostService {

    @Autowired
    private MinioUtil minioUtil;
    @Autowired
    private PostMapper postMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private FileUploadRequestServiceImpl fileUploadRequestService;

    /**
     * 发帖
     * @param postDTO
     */
    public void addPost(PostDTO postDTO) {

        //旧的文件上传方式
//        MultipartFile[] images = postDTO.getImages();
//        //存 文件在minio的地址
//        List<String> list = new ArrayList<>();
//        //判断文件是否上传图片
//        if (images != null) {
//            //如果上传了图片，将图片存入post-source,并把路径放入list
//            for (MultipartFile image : images) {
//                list.add(minioUtil.upload(image, "post-source"));
//            }
//        }
//        String s = String.join(",", list);
        //构建post实例存入数据库

        Post post = Post.builder()
                .title(postDTO.getTitle())
                .desc(postDTO.getContent())
                //.hostId(BaseContext.getCurrentUserId())   //TODO 便于调试，未检验token，暂时直接插入
                .hostId(1L)
                .image(postDTO.getImagesUrl())
                .build();

        postMapper.addPost(post);
    }

    /**
     * 评论回复
     * @param replyDTO
     */
    public void addReply(ReplyDTO replyDTO) {
        Reply reply = Reply.builder()
                .postId(replyDTO.getPostId()) //回复所属帖子id
                //.replierId(BaseContext.getCurrentUserId()) //当前回复者id //TODO 暂时定值
                .replierId(2L)
                .repliedId(replyDTO.getRepliedId())
                .content(replyDTO.getContent())
                .build();
        //插入数据库
        postMapper.addReply(reply);
        //对应贴的评论数++
        postMapper.addCommentNumber(replyDTO.getPostId());
    }

    /**
     * 分页查询帖子
     * @param page
     * @param size
     * @return
     */
    public PageResult pagePost(int page, int size) {
        PageHelper.startPage(page, size);

        Page<PostVO> postVOPage = postMapper.pagePost(page, size);

        long total = postVOPage.getTotal();
        return new PageResult(total, postVOPage.getResult());
    }

    /**
     * 获取某贴详情
     * @param postId
     * @return
     */
    public PostDetailVO getDetail(long postId) {
        Post post = postMapper.getPostById(postId);
        PostDetailVO postDetailVO = PostDetailVO.builder()
                .id(postId)
                .avatar(userMapper.getAvaById(post.getHostId()))
                .nickname(userMapper.getNicById(post.getHostId()))
                .title(post.getTitle())
                .desc(post.getDesc())
                .images(Arrays.asList(post.getImage().split(",")))
                .comments(postMapper.getReplyByPostId(postId))
                .commentNumber(post.getCommentNumber())
                .views(post.getViews())
                .creatTime(post.getCreatTime())
                .build();

        return postDetailVO;

    }


    /**
     * 删除帖子
     * @param id
     */
    @Override
    public void deletePost(Long id) {
        postMapper.deleteById(id);
    }
}
